import { Model, DataTypes } from 'sequelize';
import sequelize from '../config/database.js';

export default class Call extends Model {
  static initModel(sequelize) {
    Call.init({
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      callerId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id'
        }
      },
      receiverId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id'
        }
      },
      callId: {
        type: DataTypes.STRING(100),
        allowNull: false
      },
      status: {
        type: DataTypes.ENUM('missed', 'answered', 'rejected'),
        defaultValue: 'missed'
      },
      startTime: {
        type: DataTypes.DATE,
        allowNull: false
      },
      endTime: {
        type: DataTypes.DATE,
        allowNull: true
      },
      duration: {
        type: DataTypes.INTEGER,  // 通话时长（秒）
        defaultValue: 0
      },
      callType: {
        type: DataTypes.ENUM('normal', 'p2p'),
        defaultValue: 'normal'
      }
    }, {
      sequelize,
      modelName: 'Call',
      tableName: 'calls',
      timestamps: true
    });

    return Call;
  }
}

Call.initModel(sequelize);
